<template>
  <div class="top-category container">
    <XtxBread>
      <XtxBreadItem to="/">首页</XtxBreadItem>
      <XtxBreadItem>{{ topCategory.name }}</XtxBreadItem>
    </XtxBread>

    <XtxCarousel style="height: 500px;" :slides="home.bannerList" />
    <!-- 所有二级分类 -->
    <div class="sub-list">
      <h3>全部分类</h3>
      <ul>
        <li v-for="item in topCategory.children" :key="item.id">
          <a href="javascript:;">
            <img :src="item.picture" >
            <p>{{ item.name }}</p>
          </a>
        </li>
      </ul>
    </div>

    <!-- 分类关联商品 -->
    <div v-for="item in topCategory.children" :key="item.id" class="ref-goods">
      <div class="head">
        <h3>- {{ item.name }} -</h3>
        <p class="tag">温暖柔软，品质之选</p>
        <XtxMore />
      </div>
      <div class="body">
        <GoodsItem :goods="goods" v-for="goods in item.goods" :key="goods.id" />
      </div>
    </div>
  </div>
</template>

<script lang="ts" name="TopCategory" setup>
import useStore from '@/store';
import { storeToRefs } from 'pinia';
import { watch, watchEffect } from 'vue';
import { useRoute } from 'vue-router';
import GoodsItem from './components/GoodsItem.vue'

const route = useRoute()
const { category, home } = useStore()

// 丢失响应式, 需要借助 storeToRefs
const { topCategory } = storeToRefs(category)

if (home.bannerList.length === 0) {
  home.getBannerList()
}
// 使用 watch 是为了达成 2 个目的:
// 1. 当路由参数变化时触发发请求操作
// 2. 页面刚加载就发请求 immediate
// watch(() => route.params.id, () => {
//   category.getTopCategory(route.params.id as string)
// }, {
//   immediate: true
// })

// 而 watchEffect 可以直接达成以上两个需求 (Vue 3 的新方法)
// 作用: 
// 1. 自动监视内部用到的数据
// 2. 页面刚加载时会触发一次 类似于 watch 的 immediate
watchEffect(() => { 
  const id = route.params.id as string
  // 判断: 只有在访问分类页面时才发请求, 访问其他页面不会发请求, 例如访问首页 /   /category
  if (route.fullPath === '/category/' + id) {
    category.getTopCategory(id)
  }
})
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}

.ref-goods {
  background-color: #fff;
  margin-top: 20px;
  position: relative;
  .head {
    .xtx-more {
      position: absolute;
      top: 20px;
      right: 20px;
    }
    .tag {
      text-align: center;
      color: #999;
      font-size: 20px;
      position: relative;
      top: -20px;
    }
  }
  .body {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 0 65px 30px;
  }
}
</style>